import { Util } from "../utils/Util";

export default class LoginManager {

    private static _instance: LoginManager = null;
    public static get inst(): LoginManager {
        return this._instance || (this._instance = new LoginManager());
    }

    public isAndroid = cc.sys.isNative && cc.sys.os === cc.sys.OS_ANDROID;
    public isIOS = cc.sys.isNative && cc.sys.os === cc.sys.OS_IOS;

    private hykbCallback: Function = null;
    hykbLogin(callback: Function) {
        Util.logExt({}, "hykbLogin");
        this.hykbCallback = callback;
        if (this.isAndroid) {
            jsb.reflection.callStaticMethod("com/jingyingxiaobing/blueclient/AppActivity", "HYKBLogin", "()V");
        }
    }
    hykbLoginResult(userdata) {
        const jsonObj = JSON.parse(userdata)
        Util.logExt(jsonObj, "hykbLoginResult");
        this.hykbCallback && this.hykbCallback(jsonObj);
    }

    private dyCallback: Function = null;
    dyLogin(callback: Function) {
        Util.logExt({}, "dyLogin");
        this.dyCallback = callback;
        if (this.isAndroid) {
            jsb.reflection.callStaticMethod("com/jingyingxiaobing/blueclient/AppActivity", "DYLogin", "()V");
        }else if (this.isIOS) {
            jsb.reflection.callStaticMethod("AppController", "sendDouyinAuthRequsest");
        }
    }
    dyLoginResult(userdata) {
        const jsonObj = JSON.parse(userdata);
        Util.logExt(jsonObj, "dyLoginResult");
        this.dyCallback && this.dyCallback(jsonObj);
    }
    dyLoginResultIOS(userdata) {
        console.log("dyLoginResultIOS", userdata);
        this.dyCallback && this.dyCallback({
            userid:userdata
        });
    }

    private wxCallback: Function = null;
    wxLogin(callback: Function) {
        Util.logExt({}, "wxLogin");
        this.wxCallback = callback;
        if (this.isAndroid) {
            jsb.reflection.callStaticMethod("com/jingyingxiaobing/blueclient/AppActivity", "WXLogin", "()V");
        }else if(this.isIOS){
            jsb.reflection.callStaticMethod("AppController", "sendAuthRequest");
        }
    }
    wxLoginResult(userdata) {
        const jsonObj = JSON.parse(userdata);
        Util.logExt(jsonObj, "wxLoginResult");
        if (jsonObj.type == "ok") {
            this.wxCallback && this.wxCallback(jsonObj);
        }
    }
    wxLoginResultIOS(userdata) {
        console.log("wxLoginResultIOS", userdata);
        this.wxCallback && this.wxCallback({
            userid:userdata
        });
    }

}

cc["LoginManager"] = LoginManager;
window["LoginManager"] = LoginManager;